package com.hqyj.system.controller;

import com.hqyj.system.model.Custom;
import com.hqyj.system.model.User;
import com.hqyj.system.service.CustomService;
import frame.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/customController")
public class CustomController {

    @Autowired
    private CustomService customService;
    @RequestMapping("/list.do")
    public String List(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, Custom custom) {
        //1.接收数据;
        System.out.println("=====customController=======list()====");
        //3.调用业务;
        List<Custom> custom_dbList = customService.queryAllCustom();
//        for (Custom custom_db:custom_dbList){
//            System.out.println("======list=========" + custom_db);
//        }
        //返回数据
        model.addAttribute("custom_dbList", custom_dbList);
        //2.跳转页面;
        return "h5/custom-list";
//        return "h5/permissionList";
    }
    @RequestMapping("/add.do")
    public String add(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, Custom custom, String customIdStr) {
        //1.接收数据;

        //3.调用业务;

        int n = customService.addCustomByCustom(custom);

        //2.跳转页面;
        //查询客户信息
        List<Custom> custom_dbList = customService.queryAllCustom();
        //返回数据
        model.addAttribute("custom_dbList", custom_dbList);
        return "h5/custom-list";
    }

    @RequestMapping("/updateUI.do")
    public String updateUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, Custom custom, String customIdStr){
        //1.接收数据;
        //字符类型转成数字类型
        int customId = Integer.parseInt(customIdStr);
        //3.调用业务;
        Custom custom_db = customService.queryCustomByCustomId(customId);
        model.addAttribute("custom_db", custom_db);
        //2.跳转页面;
        return "h5/updateCustom";
    }

    @RequestMapping("/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, Custom custom, String customIdStr) {
        //1.接收数据;
        //字符类型转成数字类型
        int customId = Integer.parseInt(customIdStr);
        custom.setCustomId(customId);
        System.out.println("------UserController-------update()--------------" + custom);
        //3.调用业务;

        int n = customService.updateCustomByCustom(custom);

        //2.跳转页面;
        //查询客户信息
        List<Custom> custom_dbList = customService.queryAllCustom();
        //返回数据
        model.addAttribute("custom_dbList", custom_dbList);
        return "h5/custom-list";
    }

}
